package rad.framework.uml2.uml.edit.providers.elementItemProvider.namedElement.redefinableElement.activityEdge;

import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.ControlFlow;
import org.eclipse.uml2.uml.UMLPackage;

import rad.framework.uml2.uml.edit.UMLEditPlugin;

public class ControlFlowItemProvider
		extends ActivityEdgeItemProvider
		implements IEditingDomainItemProvider, IStructuredItemContentProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource {

	public ControlFlowItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

		}
		return itemPropertyDescriptors;
	}

	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/ControlFlow"));
	}

	@Override
	public String getText(Object object) {
		final StringBuffer text = appendType(appendKeywords(new StringBuffer(), object), "_UI_ControlFlow_type");

		final ControlFlow controlFlow = (ControlFlow) object;
		final String label = controlFlow.getLabel(shouldTranslate());

		if (label != null && label.length() > 0) {
			appendString(text, label);
		}
		if (controlFlow.getSource() != null && controlFlow.getTarget() != null) {
			text.append(" (");
			final ActivityNode src = controlFlow.getSource();
			text.append(src.getName());
			text.append(" : ");
			final ActivityNode tgt = controlFlow.getTarget();
			text.append(tgt.getName());
			text.append(')');
		}
		return text.toString();
	}

	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);
		super.notifyChanged(notification);
	}

	@Override
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);
	}

	@Override
	public String getCreateChildText(Object owner, Object feature, Object child, Collection selection) {
		final Object childFeature = feature;
		final Object childObject = child;

		final boolean qualify = childFeature == UMLPackage.Literals.NAMED_ELEMENT__NAME_EXPRESSION || childFeature == UMLPackage.Literals.ACTIVITY_EDGE__GUARD || childFeature == UMLPackage.Literals.ACTIVITY_EDGE__WEIGHT;

		if (qualify) {
			return getString("_UI_CreateChild_text2",
					new Object[] { getTypeText(childObject), getFeatureText(childFeature), getTypeText(owner) });
		}
		return super.getCreateChildText(owner, feature, child, selection);
	}

	@Override
	public ResourceLocator getResourceLocator() {
		return UMLEditPlugin.INSTANCE;
	}

}
